﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading.Tasks;

namespace WebSocketServer.Common
{
    public static class CropHelper
    {
        public static void Crop(string Img, string savepath, int Width, int Height, int X, int Y, float size)
        {
            int SaveH = 150;//固定生成宽高都为150的图片
            int SaveW = 150;
            try
            {
                using (var OriginalImage = new Bitmap(Img))
                {
                    using (var bmp = new Bitmap(SaveW, SaveH, OriginalImage.PixelFormat))
                    {
                        bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
                        using (Graphics Graphic = Graphics.FromImage(bmp))
                        {
                            Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                            Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, SaveW, SaveH), X * size, Y * size, Width * size, Height * size,
                                              GraphicsUnit.Pixel);
                            bmp.Save(savepath);

                        }
                    }
                }
            }
            catch (Exception Ex)
            {
               ;
            }
        }
    }
}
